package com.qmx.qmxapp.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.qmx.qmxapp.R;
import com.qmx.qmxapp.activity.LookMeetingDetailsActivity;
import com.qmx.qmxapp.adapter.ContactMultipleItemAdapter;
import com.qmx.qmxapp.adapter.SectionAdapter;
import com.qmx.qmxapp.base.BaseFragment;
import com.qmx.qmxapp.bean.LzyResponse;
import com.qmx.qmxapp.bean.MeetingListModel;
import com.qmx.qmxapp.callback.JsonCallback;
import com.qmx.qmxapp.constant.AppConstant;
import com.qmx.qmxapp.constant.ComParamContact;
import com.qmx.qmxapp.data.DataServer;
import com.qmx.qmxapp.data.MySection;
import com.qmx.qmxapp.data.SectionMultipleItem;
import com.qmx.qmxapp.data.Video;
import com.qmx.qmxapp.token.TokenManager;
import com.qmx.qmxapp.utils.DateTimeUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ChengXiangtong
 * @create 2018/10/29
 * @Describe 已结束
 */
public class CompletedMeetingFragment extends BaseFragment {
    private RecyclerView mRecyclerView;
    private View view;
    private Context context;
    private SectionAdapter sectionAdapter;
    private Gson mGson = new Gson();
    private List<MySection> modelList = new ArrayList<>();
    private Intent intent;

    public CompletedMeetingFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        context = getActivity();
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_completed_meeting, container, false);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(context));

        getMeetingList();
        intent = new Intent(context, LookMeetingDetailsActivity.class);
        sectionAdapter = new SectionAdapter(R.layout.layout_myschdule_item, R.layout.def_section_head, modelList);
        sectionAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                MySection mySection = modelList.get(position);
                if (mySection.isHeader)
                    Toast.makeText(context, mySection.header, Toast.LENGTH_LONG).show();
                else {
                    intent.putExtra("type", mySection.t.getType());
                    intent.putExtra("time", mySection.t.getDate());
                    intent.putExtra("content", mySection.t.getContent().toString());
                    startActivity(intent);
                    //                    Toast.makeText(context, mySection.t.getContent().toString(), Toast.LENGTH_LONG).show();
                }
            }
        });
        sectionAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(context, "长按删除", Toast.LENGTH_LONG).show();
                MySection mySection = modelList.get(position);
                //                deleteMeeting();
                return true;
            }
        });
        mRecyclerView.setAdapter(sectionAdapter);

        return view;
    }

    public void getMeetingList() {
        OkGo.<LzyResponse<MeetingListModel>>post(AppConstant.Url + ComParamContact.ReserveLists.PATH)
                .params(ComParamContact.ReserveLists.TOKEN, TokenManager.getInstance().getAuthModel().getToken())
                .execute(new JsonCallback<LzyResponse<MeetingListModel>>() {

                    @Override
                    public void onSuccess(Response<LzyResponse<MeetingListModel>> response) {
                        StringBuffer stringBuffer = new StringBuffer();
                        modelList.clear();
                        String json = mGson.toJson(response.body().data);
                        MeetingListModel meetingListModel = mGson.fromJson(json, MeetingListModel.class);
                        for (MeetingListModel.End end : meetingListModel.getEnd()) {
                            //                            Log.i("meetingListModel", wait.getTime() + "");
                            modelList.add(new MySection(true, end.getTime(), false));
                            for (MeetingListModel.End.DataX data : end.getData()) {
                                stringBuffer.setLength(0);
                                String endt = DateTimeUtils.format(data.getEndtime() * 1000, "yyyy-MM-dd HH:mm");
                                String start = DateTimeUtils.format(data.getStarttime() * 1000, "yyyy-MM-dd HH:mm");
                                stringBuffer.append(start.substring(5, 10).replace("-", "/")).append("  ").append(start.substring(11)).append(" -- ").append(endt.substring(11));
                                modelList.add(new MySection(new Video(data.getTitle(), data.getAdmin().getName(), stringBuffer.toString(), mGson.toJson(data), data.getType())));
                            }
                            //                            Log.i("MeetingListModel==", end.getTime());
                        }
                        sectionAdapter.setNewData(modelList);
                        sectionAdapter.notifyDataSetChanged();
                        //                            handleResponse(response);
                    }

                    @Override
                    public void onError(Response<LzyResponse<MeetingListModel>> response) {
                        Log.i("onError", response.getException().getMessage());
                        //                            handleError(response);
                    }
                });
    }
}
